package com.quectel.core.module.village.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.village.dao.VillageBuildingDao;
import com.quectel.core.module.village.dto.VillageBuildingDto;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.entity.VillageBuildingEntity;
import com.quectel.core.module.village.service.VillageBuildingService;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-10-18 14:40:53
 */
@DubboService
public class VillageBuildingServiceImpl implements VillageBuildingService {

    private static final Logger LOGGER = LoggerFactory.getLogger(VillageBuildingServiceImpl.class);

    private static final String VILLAGE_BUILDING_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "VILLAGE_BUILDING_CACHE_BY_ID:";

    @Autowired
    private VillageBuildingDao villageBuildingDao;
    @Autowired
    private VillageService villageService;


    @Override
    public VillageBuildingDto selectByName(Long villageId, String name) {
        VillageBuildingEntity entity = villageBuildingDao.selectOne(new LambdaQueryWrapper<VillageBuildingEntity>()
                .eq(VillageBuildingEntity::getVillageId, villageId)
                .eq(VillageBuildingEntity::getName, name)
                .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return CopyUtils.copyObj(entity, VillageBuildingDto.class);
    }

    @Override
    public List<VillageBuildingDto> selectByVillageId(Long villageId) {
        List<VillageBuildingEntity> entityList = villageBuildingDao.selectList(new LambdaQueryWrapper<VillageBuildingEntity>()
                .eq(VillageBuildingEntity::getVillageId, villageId)
        );
        return CopyUtils.copyList(entityList, VillageBuildingDto.class);
    }

    @Override
    public VillageBuildingDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                VILLAGE_BUILDING_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public VillageBuildingDto selectById(Long id) {
        VillageBuildingEntity entity = villageBuildingDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, VillageBuildingDto.class));
    }

    @Override
    public List<VillageBuildingDto> queryList(Map<String, Object> params) {
        List<VillageBuildingEntity> list = villageBuildingDao.queryList(params);
        List<VillageBuildingDto> result = CopyUtils.copyList(list, VillageBuildingDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private VillageBuildingDto paddingField(VillageBuildingDto dto) {
        if (dto == null) {
            return null;
        }
        if (dto.getVillageId() != null) {
            VillageDto villageDto = villageService.selectCacheById(dto.getVillageId());
            if (villageDto != null) {
                dto.setVillageName(villageDto.getName());
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return villageBuildingDao.queryTotal(params);
    }

    @Override
    public Long save(VillageBuildingDto dto) {
        VillageBuildingEntity entity = CopyUtils.copyObj(dto, VillageBuildingEntity.class);
        entity.setId(Snowflake.nextId());
        villageBuildingDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(VillageBuildingDto dto) {
        VillageBuildingEntity entity = CopyUtils.copyObj(dto, VillageBuildingEntity.class);
        villageBuildingDao.updateById(entity);
        CacheKit.invalidRedisCache(VILLAGE_BUILDING_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        villageBuildingDao.deleteById(id);
        CacheKit.invalidRedisCache(VILLAGE_BUILDING_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
